package com.gz.moreaseManager.modules.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.gz.moreaseManager.core.base.BaseCommonUtils;
import com.gz.moreaseManager.modules.entity.Product;
import com.gz.moreaseManager.modules.entity.ProductPhoto;
import com.gz.moreaseManager.modules.mapper.ProductMapper;
import com.gz.moreaseManager.modules.mapper.ProductPhotoMapper;
import com.gz.moreaseManager.modules.service.IProductPhotoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gz.moreaseManager.modules.transfer.PhotoFileVo;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 产品相册信息 服务实现类
 * </p>
 *
 * @author wike
 * @since 2019-02-27
 */
@Service
public class ProductPhotoServiceImpl extends ServiceImpl<ProductPhotoMapper, ProductPhoto> implements IProductPhotoService {

    @Resource
    private ProductPhotoMapper productPhotoMapper;

    @Resource
    private ProductMapper productMapper;

    @Transactional
    @Override
    public void updateProductPhoto( List<PhotoFileVo> photoFiles,Integer productId) {
        if(UtilTool.isNotBlank( photoFiles ) && photoFiles.size()>0 && UtilTool.isNotBlank( productId )){
            //获取图片base64编码
            List<String> photoList = new ArrayList<>();
            for(PhotoFileVo photoFile: photoFiles ){
                String base64Code = photoFile.getFileData();
                if( UtilTool.isNotBlank( base64Code )){
                    ProductPhoto productPhoto = new ProductPhoto();
                    String imgUrl = BaseCommonUtils.base64ToFile( base64Code );//获取上传后返回的图片
                    productPhoto.setProductImg(imgUrl);//重新存储图片路径
                    productPhoto.setProductId(productId);
                    productPhoto.setCreateTime( new Date());
                    productPhotoMapper.insert(productPhoto);
                    //当前图片设置封面
                    if( photoFile.getFileStatus() ==1){
                        Product product = new Product();
                        product.setProductId( productId );
                        product.setCovenPhoto(imgUrl );
                        productMapper.updateById(product);
                    }

                }
            }
        }
    }

    /**
     * 根据产品ID删除相册
     * @param productId
     */
    @Override
    public void deleteByProductId(Integer productId) {
        if( UtilTool.isNotBlank( productId )){
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("product_id",productId);
            productPhotoMapper.delete(wrapper);
        }
    }

    /**
     * 根据产品ID获取相册
     * @param productId
     * @return
     */
    @Override
    public List<ProductPhoto> findPhotoListByProductId(Integer productId) {
        if( UtilTool.isNotBlank( productId )){
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("product_id",productId);
            List<ProductPhoto> list = productPhotoMapper.selectList(wrapper);
            if( UtilTool.isNotBlank( list ))
                return list;
        }
        return null;
    }

    /**
     * 根据产品ID设置封面
     * @param photoId
     * @param productId
     */
    @Override
    public void setCovenPhoto(Integer photoId, Integer productId) {
        if( UtilTool.isNotBlank( photoId ) && UtilTool.isNotBlank( productId )){
            //根据id获取相册信息
            ProductPhoto productPhoto = productPhotoMapper.selectById( photoId );
            if( UtilTool.isNotBlank( productPhoto ) &&  UtilTool.isNotBlank( productPhoto.getProductImg() )){
                Product product = new Product();
                product.setProductId( productId );
                product.setCovenPhoto( productPhoto.getProductImg() );
                productMapper.updateById(product);
            }
        }
    }
}
